package com.example.blogother.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.blogother.mapper.FiredMapper;
import com.xpd.blog.bean.Fired;
import com.xpd.blog.bean.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xpd
 * @since 2024-09-22
 */
@RestController
@RequestMapping("/fired")
public class FiredController {
    @Autowired
    FiredMapper firedMapper;
    //查看该博主有没有被关注
    @RequestMapping("guan")
    public Result guan(@RequestParam int usid,@RequestParam int uid){
        Fired fired = firedMapper.selectByUsidByFid(usid, uid);
        System.out.println("fired = " + fired);
        return new  Result(1,"查询成功",fired) ;
    }
    //关注博主
    @RequestMapping("guanzhu")
    public Result guanzhu(@RequestParam int usid,@RequestParam int uid){
        int i = firedMapper.insertFired(usid, uid);
        if(i>0){
            return new  Result(1,"关注成功",null) ;
        }
        return new  Result(-1,"关注失败,请等会再试",null) ;
    }
    //取消关注
    @RequestMapping("quguanzhu")
    public Result  quguanzhu(@RequestParam int usid,@RequestParam int uid){
        int i = firedMapper.deleteByIdInt(usid, uid);
        if(i>0){
            return new  Result(1,"取消关注成功",null) ;
        }
        return new  Result(-1,"取消关注失败",null) ;
    }
    //查看自己的粉丝
    @RequestMapping("getFans")
    public Result getFans(@RequestParam int usid){
        LambdaQueryWrapper<Fired> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Fired::getFid,usid);
        List<Fired> fireds = firedMapper.selectList(lqw);
        return new Result(1,"粉丝查询成功",fireds);
    }
    //查看自己关注的博主
    @RequestMapping("getAttention")
    public Result getAttention(@RequestParam int usid){
        LambdaQueryWrapper<Fired> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Fired::getUsid,usid);
        List<Fired> fireds = firedMapper.selectList(lqw);
        return new Result(1,"自己的关注查询成功",fireds);
    }


    //查看自己的粉丝详细信息
    @RequestMapping("getAllFans")
    public Result getAllFans(@RequestParam int usid,@RequestParam int page,@RequestParam int size){
        LambdaQueryWrapper<Fired> l=new LambdaQueryWrapper<>();
        l.eq(Fired::getUsid,usid);
        List<Fired> myAttention= firedMapper.selectList(l);
        List<Fired> fireds = firedMapper.selectByUsidAndId(usid,((page-1)*size),size);
        for (int i = 0; i < myAttention.size(); i++) {
            for (int i1 = 0; i1 < fireds.size(); i1++) {
                if(myAttention.get(i).getFid()==fireds.get(i1).getUsid()&&myAttention.get(i).getUsid()==fireds.get(i1).getFid()){
                    fireds.get(i1).setFlag(true);
                }
            }
        }
        LambdaQueryWrapper<Fired> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Fired::getFid,usid);
        List<Fired> fireds1 = firedMapper.selectList(lqw);
        return new Result(1,""+fireds1.size(),fireds);
    }

    //查看自己关注的人的详细信息
    @RequestMapping("getAllAttention")
    public Result getAllAttention(@RequestParam int usid,@RequestParam int page,@RequestParam int size){
        List<Fired> fireds = firedMapper.selectByusidAttention(usid, ((page - 1) * size), size);
        LambdaQueryWrapper<Fired> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Fired::getUsid,usid);
        List<Fired> fireds1 = firedMapper.selectList(lqw);
        return new Result(1,""+fireds1.size(),fireds);
    }

    //查找粉丝数量前六的博主
    @RequestMapping("selectfired")
    public Result selectfired(@RequestParam int usid){
        List<Fired> selectfired = firedMapper.selectfired(usid);
        for (int i = 0; i < selectfired.size(); i++) {
            Fired fired = firedMapper.selectByUsidByFid(usid, selectfired.get(i).getFid());
            if (fired!=null){
                selectfired.get(i).setFlag(true);
            }
        }
        return new Result(1,"粉丝数量前六名的博主查询成功",selectfired);
    }
}

